package com.sugon.controller.device;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.entity.EnergyDevRepairRecord;
import com.sugon.entity.FaultTypeEntity;
import com.sugon.modules.device.model.vo.PerfectDevRepairRecordParamVO;
import com.sugon.service.*;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 能源设备维修记录单Controller
 *
 * @author YuXD
 * @date 2023-03-13
 */
@Controller
@RequestMapping("energyDevRepair")
public class EnergyDevRepairController {

    @Autowired
    private EnergyDevRepairRecordService energyDevRepairService;
    @Autowired
    private SysConfigService configService;
    @Autowired
    private SysDeptService deptService;
    @Resource
    private DeviceInfoService deviceService;
    @Resource
    private FaultTypeService faultTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("devmaintenrepair:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        // 维修开始时间
        String beginTime = MapUtil.getStr(params, "beginTime");
        if (StrUtil.isNotEmpty(beginTime)) {
            params.put("beginTime", DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.parseDate(beginTime))));
        }
        // 维修终止时间
        String endTime = MapUtil.getStr(params, "endTime");
        if (StrUtil.isNotEmpty(endTime)) {
            params.put("endTime", DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.parseDate(endTime))));
        }
        //查询列表数据
        Query query = new Query(params);
        List<EnergyDevRepairRecord> devMaintenRepairList = energyDevRepairService.queryList(query);
        int total = energyDevRepairService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(devMaintenRepairList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("devmaintenrepair:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        return R.ok().put("devMaintenRepair", energyDevRepairService.queryObject(id));
    }

    @NonBusinessParamValid
    @RequestMapping("/addRepairRecord")
    @RequiresPermissions("devmaintenrepair:save")
    @ResponseBody
    public R addRepairRecord(@Validated @RequestBody PerfectDevRepairRecordParamVO repairRecord, BindingResult bindingResult) {
        // 校验设备是否存在
        DeviceInfoEntity device = deviceService.queryObject(repairRecord.getDevId());
        if (device == null) {
            return R.error("设备不存在，请确认！");
        }
        // 校验设备故障是否存在
        FaultTypeEntity faultType = faultTypeService.queryObject(repairRecord.getType());
        if (faultType == null) {
            return R.error("设备故障不存在，请确认！");
        }
        // 开始维修时间不能晚于结束维修时间
        if (repairRecord.getRepairStartDate().after(repairRecord.getRepairEndDate())) {
            return R.error("维修开始时间不得晚于维修结束时间！");
        }
        EnergyDevRepairRecord devRepairRecord = new EnergyDevRepairRecord();
        BeanUtil.copyProperties(repairRecord, devRepairRecord);
        // 设置故障时间
        devRepairRecord.setFaultNum(DateUtil.between(devRepairRecord.getRepairStartDate(), devRepairRecord.getRepairEndDate(), DateUnit.MINUTE));
        devRepairRecord.setRepairUser(ShiroUtils.getUserId());
        return energyDevRepairService.save(devRepairRecord) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("devmaintenrepair:update")
    @ResponseBody
    public R update(@RequestBody EnergyDevRepairRecord devMaintenRepair) {
        // 设置故障时间
        devMaintenRepair.setFaultNum(DateUtil.between(devMaintenRepair.getRepairStartDate(), devMaintenRepair.getRepairEndDate(), DateUnit.MINUTE));
        energyDevRepairService.update(devMaintenRepair);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("devmaintenrepair:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        energyDevRepairService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<EnergyDevRepairRecord> list = energyDevRepairService.queryList(params);

        return R.ok().put("list", list);
    }

}
